package cn.sirun.com.fca.account.repo.login;

import com.tencent.cloud.iov.kernel.constant.ApiConst;
import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.kernel.service.ServiceGenerator;
import com.tencent.cloud.iov.kernel.service.interceptor.ExceptionTransformer;

import cn.sirun.com.fca.account.model.base.CarInfoDetailRequest;
import cn.sirun.com.fca.account.model.base.CarInfoDetailResponse;
import cn.sirun.com.fca.account.model.login.AuthenticateRequest;
import cn.sirun.com.fca.account.model.login.AuthenticateResponse;
import cn.sirun.com.fca.account.model.login.ChallengeRequest;
import cn.sirun.com.fca.account.model.login.ChallengeResponse;
import cn.sirun.com.fca.account.model.login.LoginRequest;
import cn.sirun.com.fca.account.model.login.LoginResponse;
import cn.sirun.com.fca.account.model.login.PersonalInfo;
import cn.sirun.com.fca.account.model.login.PersonalInfoRequest;
import cn.sirun.com.fca.account.model.login.VehicleRespons;
import io.reactivex.Observable;

public class LoginRepository {
    private static volatile LoginRepository sInstance;

    public static LoginRepository getInstance() {
        if (sInstance == null) {
            synchronized (LoginRepository.class) {
                if (sInstance == null) {
                    sInstance = new LoginRepository();
                }
            }
        }

        return sInstance;
    }

    private final LoginService mPhoneLoginService;

    private LoginRepository() {
        mPhoneLoginService = ServiceGenerator.createService(LoginService.class, ApiConst.getBaseUrl());
    }

    public Observable<BaseResponse<ChallengeResponse>> challenge(ChallengeRequest request) {
        return mPhoneLoginService.challenge(request);
    }

    public Observable<BaseResponse<LoginResponse>> passwordLogin(LoginRequest request) {
        return mPhoneLoginService.passwordLogin(request);
    }

    public Observable<BaseResponse<AuthenticateResponse>> authenticate(
            AuthenticateRequest request) {
        return mPhoneLoginService.authenticate(request);
    }

    public Observable<BaseResponse<VehicleRespons>> getVehicle() {
        return mPhoneLoginService.getVehicle();
    }

    public Observable<BaseResponse<PersonalInfo>> getPersonalInfo(PersonalInfoRequest request) {
        return mPhoneLoginService.getPersonalInfo(request);
    }

    public Observable<BaseResponse<CarInfoDetailResponse>> getCarInfoDetail(String din) {
        return mPhoneLoginService.getCarInfoDetail(new CarInfoDetailRequest(din))
                .compose(new ExceptionTransformer<CarInfoDetailResponse>());
    }

}
